package cuit.lh.remotecontrol;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Classname: FileServer
 * @Author: lh
 * @Date: 2024/4/8 22:17
 */
public class FileServer {
    public void receive() throws IOException {
        int portNumber = 8890;

        try (ServerSocket serverSocket = new ServerSocket(portNumber);
             Socket clientSocket = serverSocket.accept();
             BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
             FileOutputStream fos = new FileOutputStream("D:/receive/" + reader.readLine())) {

            byte[] mybytearray = new byte[4096];
            int bytesRead;
            while ((bytesRead = clientSocket.getInputStream().read(mybytearray, 0, mybytearray.length)) != -1) {
                fos.write(mybytearray, 0, bytesRead);
            }
            fos.flush();
            System.out.println("文件接收完成.");
        } catch (IOException e) {
            System.out.println("在尝试监听端口 " + portNumber + " 或接收连接时发生异常");
            e.printStackTrace();
        }
    }
}
